package com.boc.langxiang.presenter.common;

import android.widget.Toast;

import com.boc.langxiang.App;
import com.boc.langxiang.contract.common.AccountUpdateSexContract;
import com.boc.langxiang.entity.request.AccountUpdateSexRequest;
import com.boc.langxiang.entity.response.AccountUpdateSexContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AccountUpdateSexPresenter implements AccountUpdateSexContract.Presenter {

    private final ApiService mService;
    private AccountUpdateSexContract.View mView;

    public AccountUpdateSexPresenter(AccountUpdateSexContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }

    @Override
    public void accountUpdateSex(final String sex) {
        mView.showLoading();
        AccountUpdateSexRequest request = new AccountUpdateSexRequest();
        request.sex = sex;
        Observable<BaseResponse<AccountUpdateSexContent>> updateSex = mService.accountUpdateSex(request.params());
        updateSex.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()).subscribe(new HandleErrorSubscriber<BaseResponse<AccountUpdateSexContent>>(mView) {
            @Override
            public void onSuccess(BaseResponse<AccountUpdateSexContent> response) {
                Toast.makeText(App.getInstance(), "性别修改成功", Toast.LENGTH_SHORT).show();
                UserInfoManager.getInstance().getUserInfo().setSex(sex);
                UserInfoManager.getInstance().setToken(response.getContent().getToken());
                UserInfoManager.getInstance().setUserInfo(UserInfoManager.getInstance().getUserInfo());
                mView.endActivity();
            }
        });
    }
}
